package com.hui.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * TODO: springMVC数据绑定
 *
 * @author chenth
 * @create 2018-07-22 22:56
 **/
@Controller
public class TestControllers {

    /**
     * 基本类型数据绑定，如果没有传参数，会报500错误
     * @param age
     * @return
     * http://localhost:8080/byAge.do?age=1
     */
    @RequestMapping("/byAge.do")
    @ResponseBody
    public String age(int age){
        return "age : " + age;
    }

    /**
     * 基本类型数据绑定，可以用@RequestParam注解，value属性为传参别名，defaultValue默认赋值
     * @param age
     * @return
     * http://localhost:8080/byParamAge.do?xage=1
     */
    @RequestMapping("/byParamAge.do")
    @ResponseBody
    public String ageByParam(@RequestParam(value = "xage",defaultValue = "0") int age){
        return "age : " + age;
    }

    /**
     * 包装类型数据绑定，如果没有传参数，不会报错,直接返回null
     * @param age
     * @return
     * http://localhost:8080/byAge2.do?age=1
     */
    @RequestMapping("/byAge2.do")
    @ResponseBody
    public String age2(Integer age){
        return "age : " + age;
    }
    /**
     * 包装类型数据绑定，可以用@RequestParam注解，value属性为传参别名，defaultValue默认赋值
     * @param age
     * @return
     * http://localhost:8080/byParamAge2.do?xage=1
     */
    @RequestMapping("/byParamAge2.do")
    @ResponseBody
    public String age2ByParam(@RequestParam(value = "xage",defaultValue = "0")Integer age){
        return "age : " + age;
    }

    /**
     * 数组绑定
     * @param animal
     * @return
     * http://localhost:8080/byArray.do?animal=Tiger&animal=Monkey&animal=Rabbit
     */
    @RequestMapping("/byArray.do")
    @ResponseBody
    public String array(String[] animal){
        StringBuilder StringBuilder = new StringBuilder();
        for(String animalItem  : animal){
            StringBuilder.append(animalItem).append(" ");
        }
        return StringBuilder.toString();
    }
}
